﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Learning.ConwaysGameOfLife
{
    public class Cordinate
    {
		#region Constructors (1) 

        public Cordinate(int row, int column)
        {
            this.Row = row;
            this.Column = column;
        }

		#endregion Constructors 

		#region Properties (2) 

        public int Column { get; set; }

        public int Row { get; set; }

		#endregion Properties 

		#region Methods (9) 

		// Public Methods (6) 

        public static bool operator !=(Cordinate rhs, Cordinate lhs)
        {
            // Null check
            if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))
            {
                if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))
                {
                    // Both are null.  They do equal each other
                    return false;
                }

                // Only 1 is null the other is not so they do not equal
                return true;
            }

            return !rhs.Equals(lhs);
        }

        public static bool operator ==(Cordinate rhs, Cordinate lhs)
        {
            // Null check
            if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))
            {
                if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))
                {
                    // Both are null.  They do equal each other
                    return true;
                }

                // Only 1 is null the other is not so they do not equal
                return false;
            }

            return rhs.Equals(lhs);
        }

        public override bool Equals(object obj)
        {
            Cordinate otherObj = obj as Cordinate;

            if (otherObj == null)
            {
                return false;
            }

            return otherObj.Row == this.Row && otherObj.Column == this.Column;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public Cordinate MoveCordinate(CordinateMoveDirection rowMovement = CordinateMoveDirection.None, CordinateMoveDirection columnMovement = CordinateMoveDirection.None)
        {
            return new Cordinate(this.NewRowLocation(rowMovement), this.NewColumnLocation(columnMovement));
        }

        public override string ToString()
        {
            return string.Format("R{0}C{1}", this.Row, this.Column);
        }
		// Private Methods (3) 

        private static int DetermineNewLocation(CordinateMoveDirection direction, int oldLocation)
        {
            switch (direction)
            {
                case CordinateMoveDirection.Previous:
                    {
                        return oldLocation - 1;
                    }
                case CordinateMoveDirection.Next:
                    {
                        return oldLocation + 1;
                    }
                case CordinateMoveDirection.None:
                default:
                    {
                        return oldLocation;
                    }
            }
        }

        private int NewColumnLocation(CordinateMoveDirection direction)
        {
            return Cordinate.DetermineNewLocation(direction, this.Column);
        }

        private int NewRowLocation(CordinateMoveDirection direction)
        {
            return Cordinate.DetermineNewLocation(direction, this.Row);
        }

		#endregion Methods 
    }
}
